import 'dart:convert';
import 'dart:io';
import 'package:flutter/services.dart';
import '../../utils/k_log.dart';
//android 原生通道
class TsPlugin {

  static const MethodChannel _channel = MethodChannel('ts_flutter_plugin');

  // ///获取项目Product Flavor App
  // ///channel=>001
  // static Future<String> getChannel() async {
  //   String json = await rootBundle.loadString(
  //       "packages/${GlobalConfig.getProductConfig().assetPath}/file/project_config.json");
  //   Map<String, dynamic> map = jsonDecode(json);
  //   String channel = map["channel"];
  //   return channel;
  // }

  static Future<String> getChannel() async {
    var channel = await _channel.invokeMethod('getChannel');
    return channel;
  }

  static Future startDHAResultActivity() async {
   await _channel.invokeMethod('DHAResultActivity');
  }

  ///原生DES解析
  static Future<Map<String, dynamic>?> desDecrypt(String content) async {
    if (Platform.isIOS) {
      return null;
    } else if (Platform.isAndroid) {
      var accessPem = await _channel.invokeMethod('desDecrypt', {
        "strKey": "PKCS#8@6",
        "content": content,
      });
      KLog.d("desDecrypt ${accessPem}");
      Map<String, dynamic>? json = jsonDecode(accessPem);
      return json;
    }
    return null;
  }

  ///设备运营商，0：移动、1：联通、2：电信
  static Future<int> getSimOperator() async {
    var simOperator = await _channel.invokeMethod('getSimOperator');
    return simOperator;
  }

  static Future<String> getWebViewUserAgent() async {
    var result = await _channel.invokeMethod('getWebViewUserAgent');
    return result;
  }

  static Future<String> getAppUserAgent() async {
    var result = await _channel.invokeMethod('getAppUserAgent');
    return result;
  }

  static Future<String> getImei() async {
    var result = await _channel.invokeMethod('getImei');
    return result;
  }

  static Future<String> getPhoneMEID() async {
    var result = await _channel.invokeMethod('getPhoneMEID');
    return result;
  }

  static Future<String> getDeviceId() async {
    var result = await _channel.invokeMethod('getDeviceId');
    return result;
  }

  static Future<String> getAndroidId() async {
    var result = await _channel.invokeMethod('getAndroidId');
    return result;
  }

  static Future<String> getLocalIpV6() async {
    var result = await _channel.invokeMethod('getLocalIpV6');
    return result;
  }

  static Future<String> getSystemModel() async {
    var result = await _channel.invokeMethod('getSystemModel');
    return result;
  }

  static Future<String> getMacFromHardware() async {
    var result = await _channel.invokeMethod('getMacFromHardware');
    return result;
  }

  //2是鸿蒙
  static Future<int> getSystemCode() async {
    var result = await _channel.invokeMethod('getSystemCode');
    return result;
  }

  static Future<String> getSystemVersion() async {
    var result = await _channel.invokeMethod('getSystemVersion');
    return result;
  }

  static Future<String> getHarmonyVersion() async {
    var result = await _channel.invokeMethod('getHarmonyVersion');
    return result;
  }

  static Future<bool> isBatteryCharging() async {
    var result = await _channel.invokeMethod('isBatteryCharging');
    return result;
  }

  static Future<String> getBrand() async {
    var result = await _channel.invokeMethod('getBrand');
    return result;
  }

  static Future<int> getNetworkType() async {
    //入网类型：0：WIFI、1：1G、2：2G、3：3G、4：4G、5：5G
    var result = await _channel.invokeMethod('getNetworkType');
    switch (result.toString()) {
      case "WIFI":
        return 0;
      case "2G":
        return 2;
      case "2G":
        return 3;
      case "4G":
        return 4;
      case "5G":
        return 5;
    }
    return 0;
  }

  static Future<String> getRiskInfo() async {
    var result = await _channel.invokeMethod('getRiskInfo');
    return result;
  }

  static Future<String> getIPAddress() async {
    var result = await _channel.invokeMethod('getIPAddress');
    return result;
  }

  static Future<String> getOaId() async {
    var result = await _channel.invokeMethod('getOaId');
    if (result == null) {
      return "";
    }
    return result;
  }

  static Future<String> getOaIdU() async {
    var result = await _channel.invokeMethod('getOaidU');
    if (result == null) {
      return "";
    }
    return result;
  }

  static Future<String> getOaIdH() async {
    var result = await _channel.invokeMethod('getOaidH');
    if (result == null) {
      return "";
    }
    return result;
  }

  static void showRewardTip() async {
    await _channel.invokeMethod('showRewardTip');
  }

  static void setCheckPrivacy() async {
    await _channel.invokeMethod('setCheckPrivacy');
  }

  static void privacyAcceptFalse() async {
    await _channel.invokeMethod('privacyAcceptFalse');
  }

  static void privacyAcceptTrue() async {
    await _channel.invokeMethod('privacyAcceptTrue');
  }

  static Future<bool> isNormalUser() async {
    return await _channel.invokeMethod('isNormalUser');
  }

  static Future<String> getAppName() async {
    return await _channel.invokeMethod('getAppName');
  }

  static Future<String> getServiceUrl() async {
    return await _channel.invokeMethod('getServiceUrl');
  }

  static Future<String> getPolicyUrl() async {
    return await _channel.invokeMethod('getPolicyUrl');
  }

  static Future bDConvertInit() async {
    _channel.invokeMethod('BDConvertInit');
  }
}
